package com.godyao.mall.pms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import com.godyao.mall.pms.entity.CategoryEntity;
import com.godyao.mall.pms.service.CategoryService;
import com.godyao.mall.core.utils.PageUtils;
import com.godyao.mall.core.model.ResultMsg;



/**
 * 商品三级分类
 *
 * @author godyao
 * @email godyao0801@qq.com
 * @date 2022-04-19 14:42:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("pms/category")
public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/tree")
    public ResultMsg list(){
        List<CategoryEntity> tree = categoryService.listTree();

        return ResultMsg.resultSuccess(tree);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public ResultMsg info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return ResultMsg.resultSuccess(category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResultMsg save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return ResultMsg.resultSuccess("OK");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResultMsg update(@RequestBody CategoryEntity category){
		//categoryService.updateById(category);
		categoryService.updateCascade(category);

        return ResultMsg.resultSuccess("OK");
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public ResultMsg delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return ResultMsg.resultSuccess("OK");
    }

}
